package com.example.city_home_app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.CommunityActivity;
import com.example.city_home_app.Activity.CommunityAllActivity;
import com.example.city_home_app.Activity.PostSearchActivity;
import com.example.city_home_app.Bean.Down_Community;
import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_News;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Post;
import com.example.city_home_app.RetrofitListener;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class Fragment_community extends BaseFragment {
    @BindView(R.id.ll_community_search)
    LinearLayout llCommunitySearch;
    @BindView(R.id.img_community_main1)
    ImageView imgCommunityMain1;
    @BindView(R.id.tv_community_main1)
    TextView tvCommunityMain1;
    @BindView(R.id.img_community_main2)
    ImageView imgCommunityMain2;
    @BindView(R.id.tv_community_main2)
    TextView tvCommunityMain2;
    @BindView(R.id.img_community_main3)
    ImageView imgCommunityMain3;
    @BindView(R.id.tv_community_main3)
    TextView tvCommunityMain3;
    @BindView(R.id.img_community_main4)
    ImageView imgCommunityMain4;
    @BindView(R.id.tv_community_main4)
    TextView tvCommunityMain4;
    @BindView(R.id.img_community_main5)
    ImageView imgCommunityMain5;
    @BindView(R.id.tv_community_main5)
    TextView tvCommunityMain5;
    @BindView(R.id.img_community_main6)
    ImageView imgCommunityMain6;
    @BindView(R.id.tv_community_main6)
    TextView tvCommunityMain6;
    @BindView(R.id.img_community_main7)
    ImageView imgCommunityMain7;
    @BindView(R.id.tv_community_main7)
    TextView tvCommunityMain7;
    @BindView(R.id.img_community_main8)
    ImageView imgCommunityMain8;
    @BindView(R.id.tv_community_main8)
    TextView tvCommunityMain8;
    @BindView(R.id.tv_community_new)
    TextView tvCommunityNew;
    @BindView(R.id.tv_community_hot)
    TextView tvCommunityHot;
    @BindView(R.id.tv_community_lastback)
    TextView tvCommunityLastback;
    @BindView(R.id.rec_community_bottem)
    RecyclerView recCommunityBottem;
    Unbinder unbinder;

    private SwipeRefreshLayout refreshLayout;
    private Adapter_Post adapter_post;
    private int currentpage = 1;
    private List<Down_Community> list;

    private ApiModel model = new ApiModel();

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        recCommunityBottem = (RecyclerView) view.findViewById(R.id.rec_community_bottem);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recCommunityBottem.setLayoutManager(layoutManager);
        adapter_post = new Adapter_Post(getActivity());
        recCommunityBottem.setAdapter(adapter_post);
        recCommunityBottem.setItemAnimator(new DefaultItemAnimator());

        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_community_bottem);
        refreshLayout.setColorSchemeColors(0xff49afcd);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        model.getcommunitylist(listretrofitListener);
        SelectBottem(1);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_community;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_community_search, R.id.img_community_main1, R.id.img_community_main2, R.id.img_community_main3, R.id.img_community_main4, R.id.img_community_main5, R.id.img_community_main6, R.id.img_community_main7, R.id.img_community_main8, R.id.tv_community_new, R.id.tv_community_hot, R.id.tv_community_lastback})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.ll_community_search:
                intent = new Intent(getActivity(), PostSearchActivity.class);
                startActivity(intent);
                break;
            case R.id.img_community_main1:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(0).getCommunity_id());
                intent.putExtra("name",list.get(0).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main2:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(1).getCommunity_id());
                intent.putExtra("name",list.get(1).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main3:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(2).getCommunity_id());
                intent.putExtra("name",list.get(2).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main4:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(3).getCommunity_id());
                intent.putExtra("name",list.get(3).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main5:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(4).getCommunity_id());
                intent.putExtra("name",list.get(4).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main6:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(5).getCommunity_id());
                intent.putExtra("name",list.get(5).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main7:
                intent = new Intent(getActivity(), CommunityActivity.class);
                intent.putExtra("id",list.get(6).getCommunity_id());
                intent.putExtra("name",list.get(6).getCommunity_name());
                startActivity(intent);
                break;
            case R.id.img_community_main8:
                intent = new Intent(getActivity(), CommunityAllActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_community_new:
                SelectBottem(1);
                break;
            case R.id.tv_community_hot:
                SelectBottem(2);
                break;
            case R.id.tv_community_lastback:
                SelectBottem(3);
                break;
        }
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.getpostlist(currentpage,BaseActivity.getUserinfo().getCityid(),postlistRetrofitListener);
                        refreshLayout.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    private void SelectBottem(int type){
        tvCommunityNew.setTextColor(0x8A000000);
        tvCommunityHot.setTextColor(0x8A000000);
        tvCommunityLastback.setTextColor(0x8A000000);
        if(type == 1){
            currentpage = 1;
            tvCommunityNew.setTextColor(0xff49afcd);
        }
        if(type == 2){
            currentpage = 2;
            tvCommunityHot.setTextColor(0xff49afcd);
        }
        if(type == 3){
            currentpage = 3;
            tvCommunityLastback.setTextColor(0xff49afcd);
        }
        refreshLayout.setRefreshing(true);
        refresh();
    }

    private RetrofitListener<List<Down_Post>> postlistRetrofitListener = new RetrofitListener<List<Down_Post>>() {
        @Override
        public void onResponse(List<Down_Post> listener) {
            adapter_post.setList(listener);
            adapter_post.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<List<Down_Community>> listretrofitListener = new RetrofitListener<List<Down_Community>>() {
        @Override
        public void onResponse(List<Down_Community> listener) {
            list = listener;
            tvCommunityMain1.setText(listener.get(0).getCommunity_name());
            tvCommunityMain2.setText(listener.get(1).getCommunity_name());
            tvCommunityMain3.setText(listener.get(2).getCommunity_name());
            tvCommunityMain4.setText(listener.get(3).getCommunity_name());
            tvCommunityMain5.setText(listener.get(4).getCommunity_name());
            tvCommunityMain6.setText(listener.get(5).getCommunity_name());
            tvCommunityMain7.setText(listener.get(6).getCommunity_name());
            tvCommunityMain8.setText("全部板块");
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(0).getCommunity_pic())
                    .into(imgCommunityMain1);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(1).getCommunity_pic())
                    .into(imgCommunityMain2);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(2).getCommunity_pic())
                    .into(imgCommunityMain3);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(3).getCommunity_pic())
                    .into(imgCommunityMain4);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(4).getCommunity_pic())
                    .into(imgCommunityMain5);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(5).getCommunity_pic())
                    .into(imgCommunityMain6);
            Picasso.with(getActivity())
                    .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+listener.get(6).getCommunity_pic())
                    .into(imgCommunityMain7);
            imgCommunityMain8.setImageResource(R.drawable.community_all);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    public void onResume() {
        super.onResume();
        model.getpostlist(currentpage,BaseActivity.getUserinfo().getCityid(),postlistRetrofitListener);
    }
}
